package hoang.hut.libs.database;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class EntityHistory extends Entity<ObjectHistory>{

	public EntityHistory(Context mContext) {
		super(DBDefinition.TABLE_HISTORY, mContext);
		
	}

	@Override
	public long add(ObjectHistory obj) {
		ContentValues values = new ContentValues();
		values.put(DBDefinition.COLUMN_HISTORY_ID, obj.getId());
		values.put(DBDefinition.COLUMN_HISTORY_FROM, obj.getFrom());
		values.put(DBDefinition.COLUMN_HISTORY_TO, obj.getTo());
		values.put(DBDefinition.COLUMN_HISTORY_CONTENT, obj.getContent());
		values.put(DBDefinition.COLUMN_HISTORY_TIME, obj.getTime());
		return super.add(values);
	}

	@Override
	public boolean update(ObjectHistory obj) {
		ContentValues values = new ContentValues();
		values.put(DBDefinition.COLUMN_HISTORY_FROM, obj.getFrom());
		values.put(DBDefinition.COLUMN_HISTORY_TO, obj.getTo());
		values.put(DBDefinition.COLUMN_HISTORY_CONTENT, obj.getContent());
		values.put(DBDefinition.COLUMN_HISTORY_TIME, obj.getTime());
		String where = DBDefinition.COLUMN_HISTORY_ID + "=" + obj.getId();
		return super.update(values, where);
	}

	@Override
	public int delete(ObjectHistory obj) {
		String where = DBDefinition.COLUMN_HISTORY_ID + "="	+ obj.getId();
		return super.delete(where); 
	}

	@Override
	public ObjectHistory getById(int id) {
		ObjectHistory obj = new ObjectHistory();
		Cursor mCursor = super.select(DBDefinition.COLUMN_HISTORY_ID+"="+id);
		if (mCursor != null && mCursor.moveToFirst()) {
			obj.setId(mCursor.getInt(0));
			obj.setFrom(mCursor.getString(1));
			obj.setTo(mCursor.getString(2));
			obj.setContent(mCursor.getString(3));
			obj.setTime(mCursor.getString(4));
		} else {
			obj = null;
		}
		mCursor.close();
		return obj;
	}
	
	@Override
	public ArrayList<ObjectHistory> getAll() {
		ArrayList<ObjectHistory> listObj = new ArrayList<ObjectHistory>();
		
		Cursor mCursor = super.select(null);
		if (mCursor != null) {
			mCursor.moveToFirst();
			do {
				int id = mCursor.getInt(0);
				String from = mCursor.getString(1);
				String to = mCursor.getString(2);
				String content = mCursor.getString(3);
				String time = mCursor.getString(4);
				
				ObjectHistory history = new ObjectHistory(id, from, to, content, time);

				listObj.add(history);
			} while (mCursor.moveToNext());
		}
		mCursor.close();
		
		return listObj;
	}

}
